package 
{   
	import caurina.transitions.Tweener;
	
	import com.lib.playbook.controls.TextInput;
	import com.lib.playbook.dialog.DialogBase;
	import com.lib.playbook.events.DialogEvent;
	import com.oosterwijk.irc.FlexIrcClient;
	import com.oosterwijk.irc.User;
	import com.oosterwijk.irc.event.*;
	
	import flash.desktop.NativeApplication;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.net.SharedObject;
	import flash.utils.Timer;
	
	import qnx.ui.buttons.LabelButton;
	import qnx.ui.core.*;
	import qnx.ui.data.DataProvider;
	import qnx.ui.listClasses.DropDown;
	import qnx.ui.text.TextInput;

	public class MenuBar extends Sprite
	{
		public static const WIDTH:int = 1024;
		public static const HEIGHT:int = 250;
		public static const VISIBLE_Y:int = 0;
		public static const HIDDEN_Y:int = -HEIGHT;
		public static const SLIDE_TIME:Number = 0.5;
		/* the Flex IRC Client Object */
		private var _flexIrcClient:FlexIrcClient = null;
		private var _active:Boolean;
		
		private var _serverDP:DropDown;
		private var _connBT:LabelButton;
		private var _editBT:LabelButton;
		private var _settBT:LabelButton;
		private var _editServerDlg:EditServerDialog;
		
		private var _configArr:Array = null;
		public function MenuBar()
		{
			initializeUI();
		}
		private function initializeUI():void
		{
			_active = false;
			
			NativeApplication.nativeApplication.addEventListener(Event.EXITING,appExit)
			var so:SharedObject = SharedObject.getLocal("ircConfig");
			_configArr = so.data.array as Array;
			if(!_configArr){
				_configArr = [];
			}
			
			// set default position (HIDDEN_Y)
			this.x = 0;
			this.y = HIDDEN_Y;
			this.graphics.beginFill(0x222222,0.7);
			this.graphics.drawRect(0,0,WIDTH,HEIGHT);
			this.graphics.endFill();

			var mainContainer:Container = new Container();
			//mainContainer.margins = Vector.<Number>([2,2,2,2]);
			mainContainer.flow = ContainerFlow.HORIZONTAL;
			//mainContainer.debugColor = 0xFFCC00;
			mainContainer.padding = 10;
			
			

			_configArr.push({label: "Add new"});
			_serverDP = new DropDown();
			_serverDP.dataProvider = new DataProvider(_configArr);
			mainContainer.addChild(_serverDP);
			
			_connBT = new LabelButton();
			_connBT.label = "Connect";
			mainContainer.addChild(_connBT);
			
			_editBT = new LabelButton();
			_editBT.label = "Edit";
			_editBT.addEventListener(MouseEvent.CLICK, onEditServer);
			mainContainer.addChild(_editBT);
			
			_settBT = new LabelButton();
			_settBT.label = "Settings";
			_settBT.addEventListener(MouseEvent.CLICK, onSett);
			mainContainer.addChild(_settBT);
			
			var ti:qnx.ui.text.TextInput = new qnx.ui.text.TextInput();
			ti.width = 250;
			ti.height = 40;
			mainContainer.addChild(ti);
			
			this.addChild(mainContainer);
			mainContainer.setSize(1024, 250);
			
			
		}
		public function isVisible():Boolean
		{
			return _active;
		}
		public function show():void
		{
			_active = true;
			Tweener.addTween(this, {y:VISIBLE_Y, time:SLIDE_TIME, transition:"easeOutExpo"});
			
		}
		public function hide():void
		{
			_active = false;
			Tweener.addTween(this, {y:HIDDEN_Y, time:SLIDE_TIME, transition:"linear"});
		}
		public function processEvent(event:MouseEvent):void
		{
			if(event.localY > HEIGHT)
			{
				hide();
			}
		}
		/**
		 * Public getter and setter for the IRC Client Connection Object
		 */
		public function get flexIrcClient():FlexIrcClient
		{
			return _flexIrcClient;  
		}
		public function set flexIrcClient(value:FlexIrcClient):void
		{
			_flexIrcClient = value;
		}
		
		protected function onEditServer(event:MouseEvent):void
		{
			if(!_editServerDlg)
				_editServerDlg = new EditServerDialog();
			
			_editServerDlg.hideOnClose = false;
			_editServerDlg.addEventListener(EditServerDialogEvent.DIALOG_CLOSED, onServerEdit);
			if(_serverDP.selectedIndex < _serverDP.dataProvider.length - 1){
				var obj:Object = _serverDP.dataProvider.getItemAt(_serverDP.selectedIndex);
				_editServerDlg.reset();
				_editServerDlg.networkName = obj.label;
				_editServerDlg.address = obj.url;
				_editServerDlg.port = obj.port;
				_editServerDlg.password = obj.pass;
				_editServerDlg.index = _serverDP.selectedIndex;
			} else {
				_editServerDlg.reset();
				_editServerDlg.index = _serverDP.selectedIndex;
			}
			
			_editServerDlg.show();
		}
		private function onServerEdit(event:DialogEvent):void
		{
			var evt:EditServerDialogEvent = event as EditServerDialogEvent;
			if(evt.del){
				if(evt.index == _serverDP.dataProvider.length -1){
					return;
				}
				_serverDP.dataProvider.removeItemAt(evt.index);
				return;
			}
			var obj:Object = new Object();
			obj.label = evt.networkName;
			obj.url = evt.address;
			obj.port = evt.port;
			obj.pass = evt.password;
			if(evt.index == _serverDP.dataProvider.length - 1){
				_serverDP.dataProvider.addItemAt(obj, evt.index);
			} else {
				_serverDP.dataProvider.replaceItemAt(obj, evt.index);
			}
			_serverDP.drawNow();
		}
		protected function appExit(e:Event):void{
			var so:SharedObject = SharedObject.getLocal("ircConfig");
			var arr:Array = _serverDP.dataProvider.data;
			arr.pop();		// remove the last one "Add new"
			so.data.array = arr;
			so.flush();
		}
		protected function onSett(e:MouseEvent):void
		{
			var evt:MouseEvent = new MouseEvent(MouseEvent.MOUSE_DOWN, true, false);
			evt.localX = 200;
			evt.localY = 500;
			SharedData.instance.dispatchEvent(evt);
		}
	}
}